<?php
namespace app\admin\controller;

use app\admin\model\Fun1219Article;
use think\Request;

class FunArticle extends Base
{
    protected $model;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->model = new Fun1219Article();
    }

    public function index()
    {
        $map['isdel'] = 0;

        $search = input('get.');
        //  var_dump($search);

         $top = isset($search['top']) ? $search['top'] : 0;
         $status = isset($search['status']) ? $search['status'] : 0;
         $title = isset($search['title']) ? $search['title'] : '';
         $numofper = isset($search['numofper']) ? $search['numofper'] : 10;

        if ($title) {
            $map['title'] = ['like', '%' . $title . '%'];
            $this->assign('title', $title);
        }
        
        if ($status) {
            $map['status'] = $status;
            $this->assign('status', $status);
        }

        $data = $this->model->where($map)
            ->field('id,userid,cid,thumb,title,pubtime,sort,status,top,clicks')
            ->order('id', 'desc')
            ->paginate($numofper, false, ['query'=>request()->param()]);

      
        $this->assign('data', $data);

        return $this->fetch();
    }

    /**
     * 预览文章
     */
    public function preview($id = null)
    {
        $field = 'id, title, writer, source, keywords, summary, content, pubtime, clicks';

        $data = $this->model->field($field)->find($id);

        $this->assign('data', $data);

        return $this->fetch();
    }

    public function del()
    {
        if (Request::instance()->isAjax()) {
            $id =input('post.')['id'];

            $result = true;

            if (is_array($id)) {
                foreach ($id as $val) {
                    $list[] = ['id'=>intval($val), 'isdel'=>1];
                }
                $result = $this->model->saveAll($list);
            } else {
                $result = $this->model->save(['isdel'=>1], ['id'=>$id]);
            }

            if ($result===false) {
                return ['code' => 0, 'msg' => config('msg.1002')];
            } else {
                return ['code' => 2, 'msg' => config('msg.1001')];
            }
        }
    }

    public function topStatus()
    {
        if (Request::instance()->isAjax()) {
            $param =input('post.');
            
            $result = $this->model->save([$param['field']=>$param['value']], ['id'=>$param['id']]);

            if ($result===false) {
                return ['code' => 0];
            } else {
                return ['code' => 2];
            }
        }
    }

    public function trash()
    {
        $data = $this->model->where("isdel", 1)->field('id,title,status,pubtime')->order('id desc')->paginate();

        $this->assign('data', $data);

        return $this->fetch();
    }

    public function restore()
    {
        if (Request::instance()->isAjax()) {
            $id =input('post.id');

            if ($this->model->save(['isdel'=>0], ['id'=>$id])) {
                return ['code' => 2];
            } else {
                return ['code' => 0];
            }
        }
    }

    public function realDel()
    {
        if (Request::instance()->isAjax()) {
            $id =input('post.id');

            if ($this->model->destroy($id)) {
                return ['code' => 2];
            } else {
                return ['code' => 0];
            }
        }
    }

    public function add($cid = null)
    {
        if (Request::instance()->isPOST()) {
            $param = input('post.');
            // var_dump($param);
            // exit();
            $title = isset($param['title']) ? trim($param['title']) : '';

            if (empty($title)) {
                $this->error('标题不可为空');
            }

            $writer = isset($param['writer']) ? trim($param['writer']) : $this->username;
            $source = isset($param['source']) ? trim($param['source']) : "网络";
            $sort = isset($param['sort']) ? intval($param['sort']) : 0;
            $keywords = isset($param['keywords']) ? trim($param['keywords']) : "";
            $summary = isset($param['summary']) ? trim($param['summary']) : "";
            $content = isset($param['editorValue']) ? ($param['editorValue']) : "";
            $userid = $this->uid;
            $pubtime = time();

            try {
                $this->model->save([
                    'title' => $title,
                    'writer' => $writer,
                    'source' => $source,
                    'pubtime' => $pubtime,
                    'keywords' => $keywords,
                    'summary' => $summary,
                    'content' => $content,
                    'sort' => $sort,
                    'userid' => $userid,
                ]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误代码:" . $msg);
            }
            $this->success('添加成功', '/fun_article/index');
        } else {
            return $this->fetch();
        }
    }

    public function testTitle()
    {
        if (Request::instance()->isPOST()) {
            $param = input('post.');
            if ($this->model->where('title', $param['title'])->find()) {
                return ['code'=>2];
            } else {
                return 0;
            }
        }
    }

    public function edit($id = null)
    {
        if (Request::instance()->isPOST()) {
            $param = input('post.');
            // var_dump($param);
            // exit();
            $title = isset($param['title']) ? trim($param['title']) : '';

            if (empty($title)) {
                $this->error('标题不可为空');
            }

            $writer = isset($param['writer']) ? trim($param['writer']) : $this->username;
            $source = isset($param['source']) ? trim($param['source']) : "网络";
            $sort = isset($param['sort']) ? intval($param['sort']) : 0;
            $keywords = isset($param['keywords']) ? trim($param['keywords']) : "";
            $summary = isset($param['summary']) ? trim($param['summary']) : "";
            $content = isset($param['editorValue']) ? ($param['editorValue']) : "";
            $userid = $this->uid;
            $pubtime = time();

            try {
                $this->model->save([
                    'title' => $title,
                    'writer' => $writer,
                    'source' => $source,
                    'pubtime' => $pubtime,
                    'keywords' => $keywords,
                    'summary' => $summary,
                    'content' => $content,
                    'sort' => $sort,
                    'userid' => $userid,
                ], ['id'=>$id]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误代码:" . $msg);
            }
            $this->success('修改成功', '/fun_article/index');
        } else {
            $data = $this->model->find($id);
            
            if (!$data) {
                $this->error("文章不存在!");
            }

            $this->assign('data', $data);
            return $this->fetch();
        }
    }
}
